﻿define(["admin/app", "service/admin/authService", "service/baseService"], function (app) {
    "use strict";
    app.controller('accountController', function ($scope, $state, $rootScope, $location, w5cValidator, utils, authService, baseService) {
        w5cValidator.setRules({
            mobile: { required: '请填写手机号码', pattern: '您填写的手机号码不正确', w5cuniquecheck: '您填写的手机号码已经存在' },
            password: {
                required: "请填写密码",
                minlength: "密码长度不能小于{minlength}",
                maxlength: "密码长度不能大于{maxlength}"
            },
            re_password: {
                required: "请填写重复密码",
                repeat: "两次密码输入不一致"
            },
            code: {
                required: "请填写验证码"
            },
            shop_name: {
                required: "请填写店铺名称"
            },
            nick: {
                required: "请填写昵称"
            }
        });

        $scope.model = { mobile: '', password: '', re_password: '', code: '' };
        $scope.psdisabled = false;

        var methods = {
            login: function () {
                $scope.psdisabled = true;
                authService.login($scope.model).success(function (response) {
                    if (response.result_code == 1000) {
                        var data = response.data;
                        var currentUser = { nick: data.nick, mobile: data.mobile, role_name: data.role_name, is_admin: data.is_admin };
                        $rootScope.currentUser = currentUser;
                        $scope.psdisabled = false;
                        $state.go('app.merchant');
                    } else {
                        utils.error(response.result_msg);
                        $scope.psdisabled = false;
                    }
                }).error(function () { $scope.psdisabled = false; });
            },
            change_pwd: function () {
                $scope.psdisabled = true;
                authService.change_pwd({old_password:$scope.model.old_password, password: $scope.model.password, re_password: $scope.model.re_password }).success(function (response) {
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    $scope.psdisabled = false;
                    if (response.result_code == 1000) $scope.model = new Object();
                }).error(function () { $scope.psdisabled = false; });
            },
            profile: function () {
                authService.profile().success(function (response) {
                    $scope.model = response.data;
                });
            },
            save_profile: function () {
                authService.save_profile($scope.model).success(function (response) {
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    $scope.psdisabled = false;
                    if (response.result_code == 1000) {
                        $rootScope.currentUser.nick = $scope.model.nick;
                    }
                }).error(function () {
                    $scope.psdisabled = false;
                });
            }
        }
        if ($state.includes('app.account.profile')) {
            methods.profile();
        }

        angular.extend($scope, methods);
    });
});